package org.ainlolcat.ctp.model.data;

import org.ainlolcat.ctp.util.IDGenerator;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 03.09.12
 * Time: 13:11
 * To change this template use File | Settings | File Templates.
 */
public class Entity {
    private BigInteger id = IDGenerator.getNextID();
    private BigInteger sessionID;
    private String internalName;
    private String displayedName;
    List<List<Object>> data = new ArrayList<List<Object>>();
    Map<Integer, Column> columns = new HashMap();

    public void addColumn(){
        data.add(new ArrayList<Object>());
        int columnNumber = columnCount()-1;
        columns.put(columnNumber, new Column(this, columnNumber, "Column " + columnNumber));
    }

    public void addData(int column, Object value){
        data.get(column).add(value);
    }

    public void writeData(int column,int row, Object value){
        data.get(column).set(row,value);
    }

    public Object getData(int column, int row){
        return data.get(column).get(row);
    }

    public int columnCount(){
        return data.size();
    }

    public int rowCount(){
        if (data.isEmpty()) return 0;
        return data.get(0).size();
    }

    public void setColumn(int i, Column column){
        columns.put(i,column);
        column.setColumn(i);
        column.setEntity(this);
    }

    public Column getColumn(int i){
        return columns.get(i);
    }
    
    public void importColumn(Column column){
        addColumn();
        int cn = columnCount() - 1; //ZERO based
        int rc = column.getRowCount();
        for (int i = 0;i<rc;i++ ){
            data.get(cn).add(column.getData(i));
        }
        setColumn(cn, column.clone());
    }

    public Row getRow(int row){
        return new Row(this, row);
    }

    public BigInteger getId() {
        return id;
    }

    public BigInteger getSessionID() {
        return sessionID;
    }

    public void setSessionID(BigInteger sessionID) {
        this.sessionID = sessionID;
    }

    public String getInternalName() {
        return internalName;
    }

    public void setInternalName(String internalName) {
        this.internalName = internalName;
    }

    public String getDisplayedName() {
        return displayedName;
    }

    public void setDisplayedName(String displayedName) {
        this.displayedName = displayedName;
    }
}
